﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame1
{
    class MenuScreen : IGameScreen
    {

        public int Width { get; private set; }
        public int Height { get; private set; }

        private ContentManager _contentManager;

        private CowboyGame _cowboyGame;

        private KeyboardState oldState;

        private SpriteFont _menuFont;

        private Texture2D _mainMenuTexture;
        
        private Button _selectedButton;
        private List<Button> _buttons = new List<Button>();

        private bool gameStarted;
        
        public MenuScreen(int width, int height, ContentManager contentManager, CowboyGame cowboyGame)
        {
            Width = width;
            Height = height;
            _cowboyGame = cowboyGame;
            _contentManager = contentManager;
            _menuFont = contentManager.Load<SpriteFont>("MenuFont");
            _mainMenuTexture = _contentManager.Load<Texture2D>("Textures/mainMenu");

            Button start = new Button();
            start.Caption = "Start Game";
            start.Clicked += StartGame;
            _buttons.Add(start);

            Button exit = new Button();
            exit.Caption = "Exit";
            exit.Clicked += Exit;
            _buttons.Add(exit);
            _selectedButton = start;
        }

        public void StartGame()
        {

            CowboyGame.CurrentScreen = Screens.GameScreen;
            _cowboyGame.SuppressDraw();
            if (gameStarted)
                return;
            Button continueGame = new Button();
            continueGame.Caption = "Continue Game";
            continueGame.Clicked += Continue;
            _buttons.Add(continueGame);
            gameStarted = true;
        }

        public void Exit()
        {
            _cowboyGame.Exit();
        }

        public void Continue()
        {
            CowboyGame.CurrentScreen = Screens.GameScreen;
            _cowboyGame.SuppressDraw();
        }

        public void Update(GameTime gameTime, KeyboardState keyboardState)
        {

            if (keyboardState.IsKeyDown(Keys.Escape) && oldState.IsKeyUp(Keys.Escape))
            {
              Continue();
              
            }
            int count = 0;
            foreach (Button button in _buttons)
            {
                if (_selectedButton==button)
                    break;
                count++;
            }
            if (keyboardState.IsKeyDown(Keys.Up) && oldState.IsKeyUp(Keys.Up))
            {
                count--;
                if (count < 0)
                    count = _buttons.Count - 1;
                _selectedButton = _buttons[count];

            }
            else if (keyboardState.IsKeyDown(Keys.Down) && oldState.IsKeyUp(Keys.Down))
            {
                count++;
                if (count > _buttons.Count - 1)
                    count = 0;
                _selectedButton = _buttons[count];
            }

            if(keyboardState.IsKeyDown(Keys.Enter) && oldState.IsKeyUp(Keys.Enter))
            {
                _selectedButton.Click();
            }
            oldState = keyboardState;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            Rectangle rectangle = new Rectangle(0,0, Width,Height);
            spriteBatch.Draw(_mainMenuTexture, rectangle, Color.White );
            int i=50;
            int j=0;
            
            foreach (Button button in _buttons)
            {
                j += 50;
                Color color = Color.Red;
                if (_selectedButton == button)
                    color = Color.Black;
                spriteBatch.DrawString(_menuFont, button.Caption, new Vector2(i, j), color);
            }
            spriteBatch.End();
        }
    }
}
